<?php
	class Recipe implements JsonSerializable{
		private $id;
		private $result;
		private $materials;

		private function __construct($id, ItemStack $result, array $materials)
		{
			$this->setId($id);
			$this->setResult($result);
			$this->setMaterials($materials);
		}

		// Getters / Setters
		function getId()
		{
			return $this->id;
		}

		private function setId($newId)
		{
			$this->id = $newId;
		}

		function getResult()
		{
			return $this->result;
		}

		private function setResult($newResult)
		{
			$this->result = $newResult;
		}

		function getMaterials()
		{
			return $this->materials;
		}

		private function setMaterials(array $newMaterials)
		{
			$this->materials = $newMaterials;
		}
		// End Getters / Setters

		// Public Methods
		public function jsonSerialize()
		{
			return [
				'id' => $this->getId(),
				'result' => $this->getResult(),
				'materials' => $this->getMaterials()
			];

		}
		// End Public Methods

		// Static Methods
		public static function getRecipeForId($recipeId){
			$db = DbCfg::getConnection();

			$stmt = $db->prepare("select resultId, itemQuantity from Recipe where recipeId = ?");
			$stmt->bindValue(1, (int)$recipeId, PDO::PARAM_INT);
			$stmt->execute();
			$tmp = $stmt->fetchObject();

			if($tmp)
			{
				$stmt = $db->prepare("select itemId, quantity from RecipeMaterial where recipeId = ?");
				$stmt->bindValue(1, (int)$recipeId, PDO::PARAM_INT);
				$stmt->execute();

				$mats = array();
				while($mat = $stmt->fetchObject())
				{
					$mats[] = new ItemStack(Item::getItemForId($mat->itemId), (int)$mat->quantity);
				}

				return new Recipe(
					(int)$recipeId,
					new ItemStack(Item::getItemForId($tmp->resultId), (int)$tmp->itemQuantity),
					$mats
				);
			}
			else return null;
		}
		// End Static Methods

	}
?>
